package com.ai.trickycards.ui.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.ai.trickycards.R
import com.ai.trickycards.databinding.AdapterDataItemBinding
import com.ai.trickycards.databinding.FragmentDataBinding
import com.ai.trickycards.ui.adapter.BaseRecyclerAdapter
import com.ai.trickycards.ui.adapter.BaseViewHolder
import com.ai.trickycards.utils.Utils

class DataFragment : BaseFragment<FragmentDataBinding>() {

    override fun getViewBinding(inflater: LayoutInflater, container: ViewGroup?) = FragmentDataBinding.inflate(inflater, container, false)
    private lateinit var list: List<Pair<String, String>>

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        val userInfo = appViewModel.userInfoFlow.value
        list = listOf(
            Pair("Time played", "${Utils.formatNum(Utils.getHour(userInfo.timePlayed))}h"),
            Pair("Games played", "${userInfo.trickyRoyal + userInfo.trickyNumber + userInfo.trickyComplex}"),
            Pair("Tricky royal", "${userInfo.trickyRoyal}"),
            Pair("Tricky number", "${userInfo.trickyNumber}"),
            Pair("Tricky complex", "${userInfo.trickyComplex}"),
            Pair("Games won", "${userInfo.gamesWon}"),
            Pair("Game points", "${userInfo.score}"),
        )

        binding.recyclerView.adapter = DataAdapter()
    }

    private inner class DataAdapter : BaseRecyclerAdapter(R.layout.adapter_data_item) {
        override fun onBindViewHolder(holder: BaseViewHolder, position: Int) {
            val binding = AdapterDataItemBinding.bind(holder.itemView)
            with(list[position]) {
                binding.tvLabel.text = first
                binding.tvValue.text = second
            }
        }

        override fun getItemCount() = list.size
    }
}